package net.hpxn.gsmc.core.server;

import net.hpxn.gsmc.config.server.ServerConfigs.ServerConfig;
import net.hpxn.gsmc.core.server.exceptions.ServerCommunicationException;
import net.hpxn.gsmc.core.server.exceptions.ServerUnavailableException;
import net.sourceforge.rconed.SourceRcon;
import net.sourceforge.rconed.exception.ResponseEmpty;

public abstract class SourceServer extends Server {

	public SourceServer( ServerConfig pServerConfig ) {
		super( pServerConfig );
	}

	public abstract void updateServerStatus() throws ServerUnavailableException;

	@Override
	public String sendRaw( String pMsg ) throws ServerCommunicationException {
		log.debug( "[" + getName() + "] << " + pMsg );
		String wResponse = null;
		try {
			SourceRcon wSourceRcon = new SourceRcon();
			wResponse = wSourceRcon.send( getRemoteIp(), getRemotePort(),
					getRconPassword(), pMsg );
		} catch ( ResponseEmpty e ) {
			return null;
		} catch ( Exception e ) {
			throw new ServerCommunicationException( e );
		}
		return wResponse;
	}

	@Override
	protected String getServerStatus() throws ServerCommunicationException {
		return sendRaw( "status" );
	}

}
